package pl.un4giv3n.mightvsmagic.admin.action;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.ItemTemplate;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserType;

public class ListItemsPageAction extends PersistenceAction{

	private static final long serialVersionUID = 1L;
	private List<ItemTemplate> templates;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		EntityManager em = getEntityManager();
		User user = getSessionUser(em);
		if (user == null || user.getUserType() != UserType.ADMIN) {
			addActionError("Zaloguj się");
			em.close();
			return LOGIN;
		}
		Query query = em.createNamedQuery("find.all.itemtemplates");
		templates = query.getResultList();
		em.close();
		return super.execute();
	}

	public List<ItemTemplate> getTemplates() {
		return templates;
	}
}
